package mlProject.classifier.svm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import featureSelect.FeatureSelect;
import featureSelect.InfoGainWithSentiment;
import mlProject.DocModel;
import mlProject.DocModelReader;
import mlProject.DocModelReaderEvaluate;
import tool.ComputeIDF;

public class MyPredict {
	public static void predict(String modelPath,String evaluatePath,String resultPath) throws IOException{
		transToSvmFormat(getNumOfFeature(modelPath),evaluatePath,"src/main/resources/svm/test.txt");
		
		String[] parg = { "src/main/resources/svm/test.txt", // 这个是存放测试数据
				modelPath, // 调用的是训练以后的模型
				"src/main/resources/svm/out.txt" }; // 生成的结果的文件的路径
		svm_predict p = new svm_predict();
		try {
			p.main(parg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 调用
		File file_out = new File(resultPath);
		BufferedWriter bw_out = new BufferedWriter(new FileWriter(file_out, false));//
		
		BufferedReader reader_id = new BufferedReader(new FileReader("src/main/resources/"+evaluatePath));
		BufferedReader reader_result = new BufferedReader(new FileReader("src/main/resources/svm/out.txt"));
		String line = null;
		while ((line = reader_id.readLine()) != null) {
			String[] split = line.split(" ");
			String fileID=split[0].trim();
			
			line=reader_result.readLine();
			String result=line.trim();
			if(result.equals("1.0")) {result="positive";}
			else result="negative";

			bw_out.write(fileID+"\t"+result);
			bw_out.newLine();
		}
		bw_out.close();
		reader_id.close();
		reader_result.close();
	}
	public static void check2(String modelPath,String evaluatePath,String resultPath) throws IOException{
		File file_out = new File(resultPath);
		BufferedWriter bw_out = new BufferedWriter(new FileWriter(file_out, false));//
		
		int  temp=0;
		BufferedReader reader_id = new BufferedReader(new FileReader("src/main/resources/"+evaluatePath));
		BufferedReader reader_result = new BufferedReader(new FileReader("src/main/resources/svm/out.txt"));
		String line = null;
		while ((line = reader_id.readLine()) != null) {
			String[] split = line.split(" ");
			String fileID=split[0].trim();
			
			line=reader_result.readLine();
			String result=line.trim();
			if(result.equals("1.0")) {result="positive";temp++;}
			else result="negative";

			bw_out.write(fileID+"\t"+result);
			bw_out.newLine();
		}
		System.out.println(temp);
		bw_out.close();
		reader_id.close();
		reader_result.close();
	}
	public static void testToEvaluate(String testPath,String evaluatePath){
		ArrayList<DocModel> docs = DocModelReader.readFromFile(testPath);
		DocModel doc = null;
		File file_out = new File(evaluatePath);
		BufferedWriter bw_out;
		try {
			bw_out = new BufferedWriter(new FileWriter(file_out, false));
			for (int i = 0; i < docs.size(); i++) {
				doc = docs.get(i);
				bw_out.write(doc.id);
				for (int j = 0; j < doc.features.size(); j++) {
					String featureName = doc.features.get(j).name;
					Double featureValue = doc.features.get(j).value;
					bw_out.write(" "+featureName+":"+featureValue);
				}
				bw_out.newLine();
			}
			bw_out.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void transToSvmFormat(int numFrature,String filePathBeforeTrans,String filePathAfterTrans) {
		FeatureSelect fs = new FeatureSelect();
		ArrayList<DocModel> docs = DocModelReaderEvaluate.readFromFile(filePathBeforeTrans);///>>>>>>>>>>DocModelReaderEvaluate:no label way
		DocModel doc = null;
		ArrayList<String> feature = InfoGainWithSentiment.getFeatureSelected(numFrature);//////////>>>>>>>>>>>
		//ArrayList<String> feature = fs.getFreature("newTrain.txt",numFrature);
		Map<String, Integer> feature_index = new HashMap<String, Integer>();
		ArrayList<Double> value = new ArrayList<Double>();
		File file_out = new File(filePathAfterTrans);
		BufferedWriter bw_out;
		try {
			Integer count = 0;
			for(int i=0;i<feature.size();i++){
				feature_index.put(feature.get(i), count);
				value.add(0.0);
				count++;
				if (count == numFrature)
					break;
			}
			bw_out = new BufferedWriter(new FileWriter(file_out, false));
			for (int i = 0; i < docs.size(); i++) {
				doc = docs.get(i);
				for (int j = 0; j < value.size(); j++) {
					value.set(j, 0.0);
				}
				for (int j = 0; j < doc.features.size(); j++) {
					String featureName = doc.features.get(j).name;
					Double featureValue = doc.features.get(j).value;
					int index = 0;
					if (feature_index.containsKey(featureName)) {
						index = feature_index.get(featureName);
						value.set(index, value.get(index) + featureValue);
					}
				}
				bw_out.write("1");
				for (Integer j = 0; j < value.size(); j++) {
					bw_out.write(" " + (j + 1) + ":" + value.get(j));
				}
				bw_out.newLine();
			}
			bw_out.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public static int getNumOfFeature(String modelPath){
		Integer re=0;
		try {
		BufferedReader reader = new BufferedReader(new FileReader(modelPath));
		String line = null;
		int count=0;
			while ((line = reader.readLine()) != null) {
				count++;
				if(count==15) break;
				if(count==10){
					String[] temp=line.split(" ");
					String last=temp[temp.length-1].split(":")[0];
					re=Integer.parseInt(last);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return re;
	}
	public static void check(){
		ArrayList<DocModel> docs = DocModelReader.readFromFile("newTest.txt");
		DocModel doc = null;
		int re=0;
		BufferedReader reader;
		try {
			reader = new BufferedReader(new FileReader("src/main/resources/svm/result.list"));
			String line = null;
			int count=0;
				while ((line = reader.readLine()) != null) {
					if(line.split("\t")[1].equals(docs.get(count).label)) re++; 
					if(!line.split("\t")[0].equals(docs.get(count).id)) System.out.println("error");
					count++;
				}
		} catch ( IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(re);
		
	}
	public static void main(String[] args) throws IOException{
		//check2("src/main/resources/svm/model.txt","evaluate.txt","src/main/resources/svm/result.txt");
		//testToEvaluate("newTest.txt","src/main/resources/evaluate.txt");
		//System.out.println(getNumOfFeature("src/main/resources/svm/model.txt"));
		ComputeIDF.getIdf();
		predict("src/main/resources/svm/model.txt","evaluate.txt","src/main/resources/svm/result.list");
		check();
	}
}
